package com.gm.wholesale.module.order.view.orderManger;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.popupwindow.Popupwindow2;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.bean.WeightBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.printBlue.OrderPrintBluethManger;
import com.gm.wholesale.bluetooth.printBlue.ReceiptPrintBluethManger;
import com.gm.wholesale.constant.PrintType;
import com.gm.wholesale.module.aftersale.bean.AftersalesBean;
import com.gm.wholesale.module.aftersale.model.AfterSaleModel;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.order.bean.OrderBean;
import com.gm.wholesale.module.order.bean.OrderDetailBean;
import com.gm.wholesale.module.order.model.OrderModel;
import com.gm.wholesale.module.order.orderStatus.OrderStatusType;
import com.gm.wholesale.module.order.presenter.CreateOrderPresenter;
import com.gm.wholesale.module.order.ui.OrderMangerFragment;
import com.gm.wholesale.module.order.view.PayMoneyView;
import com.gm.wholesale.module.order.view.RefundMoneyView;
import com.gm.wholesale.module.print.bean.PrintParmter;
import com.gm.wholesale.module.print.model.PrintModel;
import com.gm.wholesale.util.ViewUtil;
import com.gm.wholesale.view.bean.PopUseBean;
import com.youth.banner.util.LogUtils;

import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

public class OrderDetailView extends LinearLayout implements View.OnClickListener {
    private static int SLEEP_TIME=200;
    private Context mContext;

    private TextView customNameTv;

    private ListView orderListView;
    private SuperTextView afterSalesView;
    private SuperTextView getMoneyView;
    private SuperTextView printTv;
    private ImageView orderStateImg;
    private TextView salesAmountTv;


    private SuperTextView mRefundView;

    private OrderDetailBean mOrderDetailBean;

    private TextView orderNumTv;
    private TextView afterSaleNumTv;
    private TextView createOrderTimeTv;
    private TextView afterSalesTimeTv;


    private LinearLayout linearLayout;


    private OrderPrintBluethManger orderPrintBluethManger;

    private ReceiptPrintBluethManger receiptPrintBluethManger;

    private OrderMangerFragment mOrderMangerFragment;
    private List<AftersalesBean> mAftersalesBean;

    private OrderBean mOrderBean;

    public void setOrderMangerFragment(OrderMangerFragment fragment) {
        mOrderMangerFragment = fragment;
    }

    public void showView() {
        //todo 有数据才显示
        if (this.mOrderDetailBean != null) {
            setVisibility(VISIBLE);
        }
    }

    public void showOrderDetail(OrderBean orderBean) {
        mOrderBean = orderBean;
        setVisibility(VISIBLE);
        showDetail();
    }


    public OrderDetailView(Context context) {
        super(context);
        initViews(context);
        orderPrintBluethManger = OrderPrintBluethManger.newInstance(context);
        receiptPrintBluethManger = ReceiptPrintBluethManger.newInstance(context);
    }

    public OrderDetailView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.fragment_order_detail, this);
        initView();
        linearLayout = new LinearLayout(getContext());

    }

    private void initView() {
        customNameTv = findViewById(R.id.customNameTv);
        orderListView = findViewById(R.id.orderListView);
        afterSalesView = findViewById(R.id.afterSalesView);
        getMoneyView = findViewById(R.id.getMoneyView);
        mRefundView = findViewById(R.id.refundMoneyView);
        printTv = findViewById(R.id.printTv);
        orderStateImg = findViewById(R.id.orderStateImg);
        salesAmountTv = findViewById(R.id.salesAmountTv);
        afterSalesView.setOnClickListener(this);
        getMoneyView.setOnClickListener(this);
        printTv.setOnClickListener(this);
        mRefundView.setOnClickListener(this);
        orderNumTv = findViewById(R.id.orderNumTv);
        afterSaleNumTv = findViewById(R.id.afterSaleNumTv);
        createOrderTimeTv = findViewById(R.id.createOrderTimeTv);
        afterSalesTimeTv = findViewById(R.id.afterSalesTimeTv);

    }

    public void showDetail() {

        OrderModel.getOrderDetail(null, mOrderBean.getOrderId(), new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(DataResp<OrderDetailBean> resp) {
                mOrderDetailBean = resp.getData();
                showView(mOrderDetailBean);
                EvenBusUtil.updateOrderMangerList(mOrderDetailBean);
            }
        });
        getAfterSales();
    }

    public void showView(OrderDetailBean orderDetailBean) {
        handleFunctionBtn(orderDetailBean);
        mOrderDetailBean = orderDetailBean;
        customNameTv.setText(orderDetailBean.getCustomerInfo().getCustomerName());
        OrderDetailBean.OrderInfoDTO orderInfoDTO = orderDetailBean.getOrderInfo();
        if (orderInfoDTO != null) {
            orderNumTv.setText("订单编号:" + orderInfoDTO.getOrderNo());
            createOrderTimeTv.setText("下单时间:" + orderInfoDTO.getOrderingTime());
        }
        //todo 商品详情数据，这里把售后数据转换成 商品数据 整合到一个集合便于 统一显示在listview
        List<OrderDetailBean.OrderDetailsDTO> orderDetailsDTOS = orderDetailBean.getOrderDetails();
        if (orderDetailsDTOS != null) {
            showGoodList(orderDetailsDTOS);
        }
        showPayInfo(orderDetailBean);

    }

    private void getAfterSales() {
        AfterSaleModel.getAfterSales(mOrderBean.getOrderId(), new BaseHttpObserver<BaseListResp<AftersalesBean>>() {
            @Override
            protected void handleResultOkResp(BaseListResp<AftersalesBean> resp) {
                mAftersalesBean = resp.getDatas();
                showAfterSales(mAftersalesBean);
            }
        });
    }

    private void showAfterSales(List<AftersalesBean> aftersaleDatas) {
        if (aftersaleDatas == null || aftersaleDatas.isEmpty()) {
            linearLayout.removeAllViews();
            orderListView.removeFooterView(linearLayout);
            return;
        }

        linearLayout.removeAllViews();
        orderListView.removeFooterView(linearLayout);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        if (aftersaleDatas != null) {
            for (AftersalesBean aftersalesBean : aftersaleDatas) {
                for (AftersalesBean.ItemsBean data : aftersalesBean.getItems()) {
                    OrderGoodInfoItemView infoItemView = new OrderGoodInfoItemView(getContext());
                    infoItemView.showView(data);
                    linearLayout.addView(infoItemView);
                }
            }
        }
        orderListView.addFooterView(linearLayout);
        if (aftersaleDatas != null && !aftersaleDatas.isEmpty()) {
            afterSaleNumTv.setVisibility(VISIBLE);
            afterSalesTimeTv.setVisibility(VISIBLE);
            afterSaleNumTv.setText("售后编号:" + aftersaleDatas.get(aftersaleDatas.size() - 1).getAftersaleNo());
            afterSalesTimeTv.setText("售后时间:" + aftersaleDatas.get(aftersaleDatas.size() - 1).getCreateTime());
        } else {
            afterSaleNumTv.setVisibility(GONE);
            afterSalesTimeTv.setVisibility(GONE);
        }

    }

    private void handleFunctionBtn(OrderDetailBean orderDetailBean) {
        if (orderDetailBean == null || orderDetailBean.getOrderInfo() == null) {
            return;
        }
        afterSalesView.setVisibility(View.GONE);
        getMoneyView.setVisibility(GONE);
        mRefundView.setVisibility(GONE);
        long paidStatus = orderDetailBean.getOrderInfo().getPaidStatus();
        //未完成
        if (paidStatus == OrderStatusType.WWC.getType()) {
            handleNotFinishOrder(orderDetailBean);
            return;
        }
        afterSalesView.setOnClickListener(this);
        afterSalesView.setCenterString("售后");

        //支付过的订单才有售后
        if (paidStatus != OrderStatusType.WWC.getType()) {
            afterSalesView.setVisibility(View.VISIBLE);
        }
        if (paidStatus == OrderStatusType.WWC.getType()) {
            getMoneyView.setVisibility(View.VISIBLE);
        }
        String debtAmount = orderDetailBean.getOrderInfo().getDebtAmount();
        if (TextUtils.isEmpty(debtAmount)
                || Double.parseDouble(debtAmount) >= 0) {
            mRefundView.setVisibility(View.GONE);
            return;
        }

    }

    private void handleNotFinishOrder(OrderDetailBean orderDetailBean) {
        //todo afterSalesView 作为修改和售后 2个按钮功能（开始没得修改，后面需求加了这里直接用这个按钮）
        afterSalesView.setCenterString("修改");
        afterSalesView.setVisibility(View.VISIBLE);
        getMoneyView.setVisibility(VISIBLE);
        //修改订单
        afterSalesView.setOnClickListener(v -> EvenBusUtil.updateOrder(orderDetailBean));

    }


    private String getStringData(String text) {
        if (!TextUtils.isEmpty(text)) {
            return "￥" + text + "元";
        }
        return "-";
    }

    //弹出打印选择，可以打印订单和小票
    private void choicePrintTypeView(View view) {
        if (mOrderDetailBean == null) return;
        ChoicePrintPopView choicePopView = new ChoicePrintPopView(mContext);
        List<PopUseBean> useBeans = new ArrayList<>();
        useBeans.add(new PopUseBean(1, "打印订单"));
        useBeans.add(new PopUseBean(2, "打印小票"));
        choicePopView.showView(useBeans);
        choicePopView.setOnChoiceUnitListener(popUseBean -> {
            if (popUseBean.getType() == PrintType.ORDER_PRINT) {
                toPrintOrderGetCmd(mOrderDetailBean);
            } else {
                toPrintReceiptGetCmd(mOrderDetailBean);
            }
        });
        Popupwindow2.showTopPopView((Activity) mContext, view, choicePopView);
    }


    private void showGoodList(List<OrderDetailBean.OrderDetailsDTO> orderDetailsDTOS) {
        orderListView.setAdapter(new CommonAdapter<>(mContext, orderDetailsDTOS, R.layout.list_order_detail_item) {
            @Override
            public void convert(ViewHolder view, OrderDetailBean.OrderDetailsDTO good) {
                view.setText(R.id.goodNameTv, good.getSkuName());
                view.setText(R.id.goodNumTv, getText(good.getQuantity()));
                view.setText(R.id.goodPriceTv, getText(good.getUnitPrice()));
                view.setText(R.id.goodUnitTv, good.getOutgoingUnitName());
                view.setText(R.id.totalPriceTv, getText(good.getSubtotal()));
                setTextColor(view, good);
            }

        });


    }

    private String getText(String text) {
        if (TextUtils.isEmpty(text)) {
            return "";
        }
        return text;
    }

    private void setTextColor(ViewHolder view, OrderDetailBean.OrderDetailsDTO good) {

        if (!good.isAfterSaleData()) {
            view.setTextColor(R.id.goodNameTv, mContext.getResources().getColor(R.color.black));
            view.setTextColor(R.id.goodNumTv, mContext.getResources().getColor(R.color.black));
            view.setTextColor(R.id.goodUnitTv, mContext.getResources().getColor(R.color.black));
            view.setTextColor(R.id.goodPriceTv, mContext.getResources().getColor(R.color.black));
            view.setTextColor(R.id.totalPriceTv, mContext.getResources().getColor(R.color.black));
            view.getView(R.id.afterSalesView).setVisibility(GONE);
        } else {
            view.getView(R.id.afterSalesView).setVisibility(View.VISIBLE);
            view.setTextColor(R.id.goodNameTv, mContext.getResources().getColor(R.color.color_F13835));
            view.setTextColor(R.id.goodNumTv, mContext.getResources().getColor(R.color.color_F13835));
            view.setTextColor(R.id.goodUnitTv, mContext.getResources().getColor(R.color.color_F13835));
            view.setTextColor(R.id.goodPriceTv, mContext.getResources().getColor(R.color.color_F13835));
            view.setTextColor(R.id.totalPriceTv, mContext.getResources().getColor(R.color.color_F13835));
        }
    }


    //todo 支付信息相关
    private void showPayInfo(OrderDetailBean orderDetailBean) {
        try {
            OrderDetailBean.OrderInfoDTO orderInfoDTO = orderDetailBean.getOrderInfo();
            if (orderInfoDTO.getPaidStatus() == OrderStatusType.YYS.getType()) {
                orderStateImg.setVisibility(View.VISIBLE);
            } else {
                orderStateImg.setVisibility(View.GONE);
            }
            salesAmountTv.setText(getStringData(orderInfoDTO.getSalesAmount()));
            //  discountAmountTv.setLeftString("优惠金额");
            //  discountAmountTv.setCenterString(getStringData(orderInfoDTO.getDiscountAmount()));
            //  aftersaleTv.setLeftString("退货金额");
            //  aftersaleTv.setCenterString(getStringData(orderInfoDTO.getAftersaleAmount()));
            //  reviceMoneyTv.setLeftString("应收");
            // reviceMoneyTv.setCenterString(getStringData(orderInfoDTO.getReceivableAmount() + ""));
            //  payMoneyTv.setLeftString("已收");
            //  payMoneyTv.setCenterString(getStringData(orderInfoDTO.getPaidAmount()));

            //  qiankuanTv.setLeftString("欠款");
            //  qiankuanTv.setCenterString(getStringData(orderInfoDTO.getDebtAmount()));

            // tuikuanTv.setLeftString("实际退款");
            //  tuikuanTv.setCenterString(getStringData(orderInfoDTO.getRefundAmount()));
            // payMethodTv.setLeftString("支付方式");
            //  payMethodTv.setCenterString(PayMethod.getByCode(orderInfoDTO.getPayment()).getMessage());
        } catch (Exception e) {

        }


    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        //打印按钮
        if (id == R.id.printTv) {
            choicePrintTypeView(view);
            return;
        }
        //收款按钮
        if (id == R.id.getMoneyView) {
            getMoney();
            return;
        }
        //售后
        if (id == R.id.afterSalesView) {
            //已经退完货物
            if (getAfterDatas() == null || getAfterDatas().isEmpty()) {
                ToastUtil.showToast("货物已经退完，无需在售后");
                return;
            }
            mOrderDetailBean.setHandelOrderDetailsDTO(getAfterDatas());
            EvenBusUtil.showAfterSale(mOrderDetailBean);
            return;
        }
        //退款
        if (id == R.id.refundMoneyView) {
            showRefunView(mOrderDetailBean);
        }
    }


    private void showPayMoneyView(OrderDetailBean data) {
        PayMoneyView payMoneyView = new PayMoneyView(mContext);
        payMoneyView.setOnSubmitClickListener((payInfoBean, goodBeans) -> CreateOrderPresenter.subMitCollection(mContext, data, payInfoBean, new PayMoneyView.OnCreateOrderListener() {
            @Override
            public void onCreateSuccess() {
                showOrderDetail(mOrderBean);
                Popupwindow.hiddenPop();
            }
        }));
        if (data.getOrderInfo() != null) {
            OrderDetailBean.OrderInfoDTO orderInfoDTO = data.getOrderInfo();
            double salesAmount = Double.parseDouble(orderInfoDTO.getSalesAmount());
            double needPayMoney = Double.parseDouble(orderInfoDTO.getSalesAmount());
            payMoneyView.showViewByGetMoney(salesAmount, needPayMoney);
            Popupwindow.showPopCenterView((Activity) mContext, payMoneyView);
        } else {
            ToastUtil.showToast("数据异常");
        }


    }

    public void showRefunView(OrderDetailBean data) {
//        RefundMoneyView refundMoneyView = new RefundMoneyView(mContext);
//        refundMoneyView.setAct((MainActivity) mContext);
//        refundMoneyView.showView(data);
//        Popupwindow.showPopCenterView((Activity) mContext, refundMoneyView);
    }

    //打印订单 调用接口获取打印机 指令
    private void toPrintOrderGetCmd(OrderDetailBean orderDetailBean) {
        if (orderDetailBean == null) {
            return;
        }
        if (orderPrintBluethManger.getConnectionState() == 3) {
            LogUtils.d("已连接，直接打印");
            toPrint(orderDetailBean);
            return;
        }
        String devcieAddress = DeviceManger.getDeviceManger().getPrintDeviceAddress();
        orderPrintBluethManger.startConnection((Activity) mContext, devcieAddress, new OnConnectionBluListener() {
            @Override
            public void connection(DeviceBean deviceBean) {
                LoadingDialog.newLoadingDialog().hid();
                toPrint(orderDetailBean);
            }

            @Override
            public void connectionFail() {
                LoadingDialog.newLoadingDialog().hid();
                ViewUtil.showPrintError();
                DeviceManger.getDeviceManger().cleanPrintDevice();
            }

            @Override
            public void getNumber(WeightBean weightBean) {

            }
        });


    }

    private void toPrint(OrderDetailBean orderDetailBean) {
        DeviceManger.getDeviceManger().savePrintDevice(orderPrintBluethManger.getConnectionDevice(mContext));
        ToastUtil.showToast("连接打印机成功，开始打印");
        LoadingDialog.newLoadingDialog().hid();
        PrintParmter printParmter = new PrintParmter();
        List<String> datas = new ArrayList<>();
        datas.add(orderDetailBean.getOrderInfo().getOrderNo());
        printParmter.setOrderNos(datas);
        PrintModel.print((IBaseView) mContext, printParmter, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BasePageData<String> resp) {
                if (resp != null && resp.getData() != null
                        && resp.getData().getDatas() != null
                        && !resp.getData().getDatas().isEmpty()) {
                    for (String data : resp.getData().getDatas()) {
                       orderPrintBluethManger.print(data);
                    }

                } else {
                    ToastUtil.showToast("获取打印指令失败 请重试");
                }

            }
        });
    }


    //打印小票 调用接口获取打印机 指令
    private void toPrintReceiptGetCmd(OrderDetailBean orderDetailBean) {
        if (orderDetailBean == null) {
            return;
        }
        if (receiptPrintBluethManger.getConnectionState() == 3) {
            toPrintReceipt(orderDetailBean);
            return;
        }
        String device = DeviceManger.getDeviceManger().getReceiPrintAddress();
        receiptPrintBluethManger.startConnection((Activity) mContext, device, new OnConnectionBluListener() {
            @Override
            public void connection(DeviceBean deviceBean) {
                LoadingDialog.newLoadingDialog().hid();
                toPrintReceipt(orderDetailBean);
            }

            @Override
            public void connectionFail() {
                LoadingDialog.newLoadingDialog().hid();
                ViewUtil.showPrintError();
                DeviceManger.getDeviceManger().cleanReceiPrintDevice();
            }

            @Override
            public void getNumber(WeightBean weightBean) {

            }
        });


    }

    private void toPrintReceipt(OrderDetailBean orderDetailBean) {
        DeviceManger.getDeviceManger().saveReceiPrintDevice(orderPrintBluethManger.getConnectionDevice(mContext));
        ToastUtil.showToast("连接打印机成功，开始打印");
        LoadingDialog.newLoadingDialog().hid();
        PrintParmter printParmter = new PrintParmter();
        List<String> datas = new ArrayList<>();
        datas.add(orderDetailBean.getOrderInfo().getOrderNo());
        printParmter.setOrderNos(datas);
        PrintModel.printReceipt((IBaseView) mContext, printParmter, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BasePageData<String> resp) {
                if (resp != null && resp.getData() != null
                        && resp.getData().getDatas() != null
                        && !resp.getData().getDatas().isEmpty()) {
                    for (String data : resp.getData().getDatas()) {
                        if (receiptPrintBluethManger != null) {
                            receiptPrintBluethManger.print(data);
                        }
                    }

                } else {
                    ToastUtil.showToast("获取打印指令失败请重试");
                }

            }
        });
    }

    private void getMoney() {
        List<OrderDetailBean.OrderDetailsDTO> orderDetailsDTOS = mOrderDetailBean.getOrderDetails();
        if (orderDetailsDTOS == null) return;
        for (OrderDetailBean.OrderDetailsDTO orderDetailsDTO : orderDetailsDTOS) {
            String num = orderDetailsDTO.getQuantity();
            String price = orderDetailsDTO.getUnitPrice();
            if (TextUtils.isEmpty(num) || Double.parseDouble(num) <= 0) {
                ToastUtil.showToast(orderDetailsDTO.getSkuName() + "数量未填写，请先修改");
                return;
            }
            if (TextUtils.isEmpty(price) || Double.parseDouble(price) <= 0) {
                ToastUtil.showToast("货物： " + orderDetailsDTO.getSkuName() + "价格未填写，请先修改");
                return;
            }
        }
        showPayMoneyView(mOrderDetailBean);
    }

    //todo 获取未售后的商品数据和数量（原商品数量-已售后的商品数量）
    private List<OrderDetailBean.OrderDetailsDTO> getAfterDatas() {

        List<OrderDetailBean.OrderDetailsDTO> goods = mOrderDetailBean.getOrderDetails();
        List<OrderDetailBean.OrderDetailsDTO> afterSaleGoods = new ArrayList<>();
        List<OrderDetailBean.OrderDetailsDTO> newGoods = new ArrayList<>();
        if (goods == null) return null;
        //todo 先把全部售后的商品遍历出来，放一个数组
        if (mAftersalesBean != null && !mAftersalesBean.isEmpty()) {
            for (AftersalesBean aftersaler : mAftersalesBean) {
                List<AftersalesBean.ItemsBean> itemsDTOS = aftersaler.getItems();
                if (itemsDTOS == null) {
                    continue;
                }
                for (AftersalesBean.ItemsBean itemsDTO : itemsDTOS) {
                    OrderDetailBean.OrderDetailsDTO detailsDTO = new OrderDetailBean.OrderDetailsDTO();
                    detailsDTO.setOrderLineId(itemsDTO.getOrderLineId());
                    detailsDTO.setQuantity(itemsDTO.getQuantity());
                    afterSaleGoods.add(detailsDTO);
                }
            }
        }

        if (afterSaleGoods != null && !afterSaleGoods.isEmpty()) {
            //todo 遍历商品和已经售后的商品数据，做减法操作，把已经售后的数量减去。得到还未售后的商品数量
            for (OrderDetailBean.OrderDetailsDTO good : goods) {
                // todo 详情界面 把售后数据也装在了这个数组里面，这里做一个过滤
                if (good.isAfterSaleData()) {
                    continue;
                }
                OrderDetailBean.OrderDetailsDTO newGood = new OrderDetailBean.OrderDetailsDTO();
                newGood.setSkuName(good.getSkuName());
                newGood.setQuantity(good.getQuantity());
                newGood.setUnitPrice(good.getUnitPrice());
                newGood.setOrderingUnitName(good.getOrderingUnitName());
                newGood.setOrderLineId(good.getOrderLineId());
                newGood.setOutgoingUnitName(good.getOutgoingUnitName());
                newGood.setOrderingUnitName(good.getOrderingUnitName());
                double oldNum = Double.parseDouble(good.getQuantity());

                for (OrderDetailBean.OrderDetailsDTO orderDetailsDTO : afterSaleGoods) {
                    if (orderDetailsDTO.getOrderLineId() == good.getOrderLineId()) {
                        try {
                            double afterGoodNum = Double.parseDouble(orderDetailsDTO.getQuantity());
                            double newNum = NumberUtils.subDouble(oldNum, afterGoodNum);
                            oldNum = newNum;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                //大于0才增加进去，小于等于说明 该商品已经售后完毕，退货完了。就不显示在列表里面
                if (oldNum > 0) {
                    newGood.setQuantity(NumberUtils.subZeroAndDot(oldNum + ""));
                    newGoods.add(newGood);
                }
            }
            return newGoods;
        } else {
            // 没有售后数据，说明第一，直接显示。不需要对数据进行减法操作
            return mOrderDetailBean.getOrderDetails();
        }


    }


}

